<template>
  <div class="publish-container">
    <el-card class="publish-card">
      <template #header>
        <h2>发布二手商品</h2>
      </template>
      <el-form
        :model="productForm"
        status-icon
        :rules="rules"
        ref="publishForm"
        label-width="120px"
        class="publish-form"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="productForm.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model.number="productForm.price" placeholder="请输入商品价格" type="number" />
        </el-form-item>
        <el-form-item label="商品描述" prop="description">
          <el-input
            v-model="productForm.description"
            type="textarea"
            placeholder="请输入商品描述"
            :rows="4"
          />
        </el-form-item>
        <el-form-item label="商品图片" prop="images">
          <el-upload
            action="#"
            :auto-upload="false"
            :on-change="handleImageChange"
            :file-list="productForm.images"
            list-type="picture-card"
          >
            <template #trigger>
              <el-icon><Plus /></el-icon>
            </template>
          </el-upload>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">发布商品</el-button>
          <el-button @click="resetForm">重置表单</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Plus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

const publishForm = ref(null);
const productForm = ref({
  name: '',
  price: null,
  description: '',
  images: []
});

const rules = {
  name: [
    { required: true, message: '请输入商品名称', trigger: 'blur' }
  ],
  price: [
    { required: true, message: '请输入商品价格', trigger: 'blur' },
    { type: 'number', message: '价格必须为数字', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入商品描述', trigger: 'blur' }
  ],
  images: [
    { required: true, message: '请上传商品图片', trigger: 'change' }
  ]
};

const handleImageChange = (file, fileList) => {
  productForm.value.images = fileList;
};

const submitForm = () => {
  publishForm.value.validate((valid) => {
    if (valid) {
      // 这里可以添加实际的提交逻辑，例如发送请求到后端
      ElMessage.success('商品发布成功');
      console.log('提交的商品信息:', productForm.value);
    } else {
      ElMessage.error('请完善商品信息');
    }
  });
};

const resetForm = () => {
  publishForm.value.resetFields();
  productForm.value.images = [];
};
</script>

<style scoped>
.publish-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f5f5;
  background: url('/src/static/背景图.jpg') no-repeat center center;
  background-size: cover;
}

.publish-card {
  width: 100%;
  max-width: 600px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.publish-form {
  padding: 20px;
}
</style>
    